package Offer2017;

/**
 * 从1到N整数中，1出现的次数
 * Created by 雷文康 on 2018/1/1.
 */
public class Test32 {
    public static void main(String[] args) {
        System.out.println(numberOf1(10));
    }
    public static int numberOf1(int n){
        if (n <= 0){
            return 0;
        }
        int count = 0;
        for (int i = 1; i <= n; i++) {
            count += countOf1(i);
        }
        return count;
    }

    private static int countOf1(int num){
        int count = 0;
        while (num != 0) {
            if (num % 10 == 1) {
                count++;
            }
            num /= 10;
        }
        return count;
    }
}
